package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.DtsCategory;
import com.qf.service.DtsCategoryService;
import com.qf.util.ResponseUtil;
import com.qf.vo.CatVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.naming.Name;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/category")
public class DtsAdminCategory {

    @Autowired
    DtsCategoryService categoryService;

    @RequestMapping("/delete")
    public Object delete(@RequestBody DtsCategory category){
        categoryService.delete(category);
        return ResponseUtil.ok();
    }


    @RequestMapping("/update")
    public Object update(@RequestBody DtsCategory category){
        categoryService.update(category);
        return ResponseUtil.ok();
    }

    @RequestMapping("create")
    public Object create(@RequestBody DtsCategory category){
        DtsCategory category1=categoryService.add(category);
        return ResponseUtil.ok(category1);
    }

    @RequestMapping("/l1")
    public Object l1(){
        List<CatVo> catVos=categoryService.findLv1();
        return ResponseUtil.ok(catVos);

    }
    @RequestMapping("/list")
    public Object list(@RequestParam(name = "page",required = false,defaultValue = "1") Integer page,
                       @RequestParam(name = "limit",required = false,defaultValue = "20")Integer limit,
                       String sort,String order,Integer id,String name){

        IPage<DtsCategory> iPage =categoryService.findByPage(page,limit,sort,order,id,name);
        Map<String,Object> data = new HashMap<>();
        data.put("total",iPage.getTotal());
        data.put("items",iPage.getRecords());
        return ResponseUtil.ok(data);
    }
}
